<template>
  <div class="price-statistics">
    <el-card shadow="never" class="stats-card">
      <template #header>
        <div class="card-header">
          <span>价格统计</span>
          <el-tag :type="trendTagType" size="small">
            <el-icon style="vertical-align: middle;">
              <component :is="trendIcon" />
            </el-icon>
            {{ trendText }}
          </el-tag>
        </div>
      </template>

      <div class="stats-grid">
        <div class="stat-item current">
          <div class="stat-label">当前价格</div>
          <div class="stat-value">
            ¥{{ currentPrice.toFixed(2) }}
            <el-tag v-if="statistics.is_lowest_recently" type="success" size="small" class="lowest-tag">
              近期最低
            </el-tag>
          </div>
          <div class="stat-change" :class="changeClass">
            {{ changeText }}
          </div>
        </div>

        <div class="stat-item">
          <div class="stat-label">最高价</div>
          <div class="stat-value price-high">¥{{ statistics.highest_price.toFixed(2) }}</div>
          <div class="stat-sub">
            比当前高 ¥{{ (statistics.highest_price - currentPrice).toFixed(2) }}
          </div>
        </div>

        <div class="stat-item">
          <div class="stat-label">最低价</div>
          <div class="stat-value price-low">¥{{ statistics.lowest_price.toFixed(2) }}</div>
          <div class="stat-sub">
            比当前低 ¥{{ (currentPrice - statistics.lowest_price).toFixed(2) }}
          </div>
        </div>

        <div class="stat-item">
          <div class="stat-label">平均价</div>
          <div class="stat-value price-avg">¥{{ statistics.average_price.toFixed(2) }}</div>
          <div class="stat-sub">
            {{ currentPrice > statistics.average_price ? '高于' : '低于' }}平均
            ¥{{ Math.abs(currentPrice - statistics.average_price).toFixed(2) }}
          </div>
        </div>
      </div>

      <el-divider />

      <div class="advice-section">
        <el-alert :title="adviceTitle" :type="adviceType" :closable="false" show-icon>
          <template #default>
            {{ adviceContent }}
          </template>
        </el-alert>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { ArrowUp, ArrowDown, Minus } from '@element-plus/icons-vue'
import type { PriceStatistics as PriceStats } from '@/api/product'

interface Props {
  statistics: PriceStats
  currentPrice: number
}

const props = defineProps<Props>()

// 趋势图标
const trendIcon = computed(() => {
  switch (props.statistics.price_trend) {
    case 'up':
      return ArrowUp
    case 'down':
      return ArrowDown
    default:
      return Minus
  }
})

// 趋势标签类型
const trendTagType = computed(() => {
  switch (props.statistics.price_trend) {
    case 'up':
      return 'danger'
    case 'down':
      return 'success'
    default:
      return 'info'
  }
})

// 趋势文本
const trendText = computed(() => {
  const rate = Math.abs(props.statistics.change_rate)
  switch (props.statistics.price_trend) {
    case 'up':
      return `上涨 ${rate.toFixed(1)}%`
    case 'down':
      return `下降 ${rate.toFixed(1)}%`
    default:
      return '稳定'
  }
})

// 变化类样式
const changeClass = computed(() => {
  if (props.statistics.change_rate > 0) return 'up'
  if (props.statistics.change_rate < 0) return 'down'
  return 'stable'
})

// 变化文本
const changeText = computed(() => {
  const rate = props.statistics.change_rate
  if (rate > 0) return `↑ ${rate.toFixed(2)}%`
  if (rate < 0) return `↓ ${Math.abs(rate).toFixed(2)}%`
  return '持平'
})

// 购买建议
const adviceType = computed(() => {
  if (props.statistics.is_lowest_recently) return 'success'
  if (props.statistics.price_trend === 'down') return 'success'
  if (props.statistics.price_trend === 'up') return 'warning'
  return 'info'
})

const adviceTitle = computed(() => {
  if (props.statistics.is_lowest_recently) return '💰 购买建议：推荐购买'
  if (props.statistics.price_trend === 'down') return '📉 价格趋势：下降中'
  if (props.statistics.price_trend === 'up') return '📈 价格趋势：上涨中'
  return '📊 价格趋势：稳定'
})

const adviceContent = computed(() => {
  if (props.statistics.is_lowest_recently) {
    return '当前价格接近历史最低价，是入手的好时机！'
  }
  if (props.statistics.price_trend === 'down') {
    return '价格呈下降趋势，可以考虑再观望一下或现在购买。'
  }
  if (props.statistics.price_trend === 'up') {
    return '价格呈上涨趋势，建议尽快购买以避免涨价。'
  }
  return '价格相对稳定，可根据需求决定购买时机。'
})
</script>

<style scoped>
.price-statistics {
  width: 100%;
}

.stats-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 500;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin: 20px 0;
}

.stat-item {
  text-align: center;
  padding: 15px;
  border-radius: 8px;
  background: #f5f7fa;
  transition: all 0.3s;
}

.stat-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.stat-item.current {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.stat-label {
  font-size: 13px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-item.current .stat-label {
  color: rgba(255, 255, 255, 0.8);
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.stat-item.current .stat-value {
  color: white;
}

.price-high {
  color: #f56c6c;
}

.price-low {
  color: #67c23a;
}

.price-avg {
  color: #409eff;
}

.stat-change {
  font-size: 14px;
  font-weight: 500;
}

.stat-change.up {
  color: rgba(255, 255, 255, 0.9);
}

.stat-change.down {
  color: rgba(255, 255, 255, 0.9);
}

.stat-change.stable {
  color: rgba(255, 255, 255, 0.7);
}

.stat-sub {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

.lowest-tag {
  font-size: 12px;
  margin-left: 8px;
}

.advice-section {
  margin-top: 15px;
}
</style>

